package com.zhihuiyanche.controller.users;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zhihuiyanche.common.util.TokenUtil;
import com.zhihuiyanche.domain.Shops;
import com.zhihuiyanche.domain.Users;
import com.zhihuiyanche.service.shops.ShopsService;
import com.zhihuiyanche.service.users.UsersService;

@RestController
public class UserController
{
	@Autowired
	private UsersService	usersService;

	@Autowired
	private ShopsService	shopsService;
	
	@Autowired  
    private TokenUtil tokenUtil; //引用统一的参数配置类  
	
	/**
	 * 注册
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "insertUser", method ={ RequestMethod.POST, RequestMethod.GET })
	public ModelAndView insertUser(Users users, Shops shops, HttpServletRequest req, HttpServletResponse resp, ModelMap context)
	{
		ModelAndView mv = null;
		if(!tokenUtil.TOKEN.equals(users.getToken())){
			mv = new ModelAndView("sign-up");
			return mv;
		}
		String shopId = usersService.insertUsers(users);
		if (shopId.length() == 2)
		{
			mv = new ModelAndView("sign-up");
			return mv;
		}

		mv = new ModelAndView("sign-in");
		shops.setId(shopId);
		shopsService.insertShops(shops);

		return mv;
	}

	/**
	 * 登录
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "getUser", method =	{ RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody String getUser(Users users, HttpServletRequest req, HttpServletResponse resp, HttpSession session, ModelMap context)
	{
		// Result result = new Result();
		List<Users> usersList = usersService.getUsersByCondition(users);
		String result = "";
		if (usersList.size() > 0)
		{
			Users user = usersList.get(0);
			Shops shops = new Shops();
			shops.setId(user.getShopId());
			Shops shop = shopsService.getShopsById(shops);
			if(shop != null){
				if(shop.getIsValid() == 0){
					return "2";//店铺未激活
				}
			}else{
				
				return "0";//查不到店铺认为用户不存在
				
			}
			session.setAttribute("shopId", user.getShopId());
			session.setAttribute("username", user.getUsername());
			return "1";//验证成功
		}
		else
		{
			result = "0";//用户名或密码错误
		}
		return result;
	}
	/**
	 * 判断用户名唯一性
	 * 
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "getUserOnly", method =	{ RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody String getUserOnly(Users users, HttpServletRequest req, HttpServletResponse resp, HttpSession session, ModelMap context)
	{
		List<Users> usersList = usersService.getUsersByCondition(users);
		String result = "";
		if (usersList.size() > 0)
		{
			result = "1";
		}
		else
		{
			result = "0";
		}
		return result;
	}
	
	/**
	 * 修改密码
	 * 
	 * @param password
	 * @param req
	 * @param resp
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "updatePassword", method =	{ RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody String updatePassword(String password,String oldPassword, HttpServletRequest req, HttpServletResponse resp, HttpSession session, ModelMap context)
	{
		String result = "";
		Users users = new Users();
		users.setUsername((String)session.getAttribute("username"));
		users.setPassword(oldPassword);
		List<Users> usersList = usersService.getUsersByCondition(users);
		if(usersList.size()<1){
			result = "2";//修改成功，清除session
			return result;
		}
		users.setPassword(password);
		boolean updateStatus = usersService.updatePassword(users);
		if(updateStatus){
			result = "1";//修改成功，清除session
			session.invalidate();
		}else{
			result = "0";//修改失败
		}
		return result;
	}

}